package com.example.administrator.runbook.findfragment.adapter;

import android.content.Context;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.model.AudioBooksDetail;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/11/10.
 */

public class AudioBooksDetailAdapter extends BaseAdapter {

    private Context mContext;
    private List<AudioBooksDetail.Details> mList;
    private LayoutInflater mLayoutInflater;
    private String mIntro;

    public AudioBooksDetailAdapter(Context context, List<AudioBooksDetail.Details> list) {
        mContext = context;
        mList = list;
        mLayoutInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        Log.d("getCount", "getCount: " + mList.size());
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = convertView;

        // 1.视图复用
        if (ret == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ret = inflater.inflate(R.layout.audio_books_detail_item, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {
            holder = new ViewHolder();
            holder.bookImg = (ImageView) ret.findViewById(R.id.audio_book_img);
            holder.title = (TextView) ret.findViewById(R.id.audio_book_title);
            holder.info = (TextView) ret.findViewById(R.id.audio_book_info);
            holder.favoriteCount = (TextView) ret.findViewById(R.id.audio_book_favorite_count);
            holder.trackCount = (TextView) ret.findViewById(R.id.audio_book_track_count);
            ret.setTag(holder);
        }

        onBindView(holder, mList.get(position));

        return ret;
    }

    private void onBindView(ViewHolder holder, AudioBooksDetail.Details details) {
        Picasso.with(mContext)
                .load(details.getImg())
                .fit()
                .into(holder.bookImg);
        holder.title.setText(details.getTitle());
        mIntro = details.getIntro();
        if (!mIntro.equals("")) {
            holder.info.setText(mIntro);
        } else {
            holder.info.setText("作者太懒，什么都没写！");
        }
        holder.favoriteCount.setText(Long.toString(details.getPlayCount()/1000) + " 人在追");
        holder.trackCount.setText("共 " + Long.toString(details.getTrackCount()) + " 集");
    }

    private static class ViewHolder {
        private ImageView bookImg;
        private TextView title;
        private TextView info;
        private TextView favoriteCount;
        private TextView trackCount;
    }
}
